﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LayersDotNet.DataAccess.Interface;
using LayersDotNet.DataAccess;
using LayersDotNet.DataAccess.EntityFx;
using LayersDotNet.BusinessLogic;
using System.Web;

namespace Common.InstanceManager
{
    public class WebApp
    {
        private static IDataAccessAccessPoint _dataAccessAccessPoint;

        public static IDataAccessAccessPoint DataAccess
        {
            get
            {
                _dataAccessAccessPoint = null;

                if (HttpContext.Current != null)
                    if (HttpContext.Current.Session["DataLibrary"] != null)
                        if (HttpContext.Current.Session["DataLibrary"] is IDataAccessAccessPoint)
                        {
                            _dataAccessAccessPoint = HttpContext.Current.Session["DataLibrary"] as IDataAccessAccessPoint;
                            _dataAccessAccessPoint.Refresh();
                        }

                if (_dataAccessAccessPoint == null)
                {
                    _dataAccessAccessPoint = GetNewDataLibrary();
                    RemoveFromSession(true, true);

                    SaveToSession(true, false);
                }

                return _dataAccessAccessPoint;
            }
            set
            {
                _dataAccessAccessPoint = value;

                RemoveFromSession(true, true);

                SaveToSession(true, false);
            }
        }

        private static void SaveToSession(bool saveDL, bool saveBL)
        {
            if (HttpContext.Current != null)
            {
                if (saveDL)
                    HttpContext.Current.Session["DataLibrary"] = _dataAccessAccessPoint;

                if (saveBL)
                    HttpContext.Current.Session["BusinessLogic"] = _businessLogicAccessPoint;
            }
        }

        private static void RemoveFromSession(bool removeDL, bool removeBL)
        {
            if (HttpContext.Current != null)
            {
                if (removeDL)
                    HttpContext.Current.Session["DataLibrary"] = null;

                if (removeBL)
                    HttpContext.Current.Session["BusinessLogic"] = null;
            }
        }

        private static IBusinessLogicAccessPoint _businessLogicAccessPoint;

        public static IBusinessLogicAccessPoint BusinessLogic
        {
            get 
            {
                _businessLogicAccessPoint = null;

                if (HttpContext.Current != null)
                    if (HttpContext.Current.Session["BusinessLogic"] != null)
                        if (HttpContext.Current.Session["BusinessLogic"] is IBusinessLogicAccessPoint)
                        {
                            _businessLogicAccessPoint = HttpContext.Current.Session["BusinessLogic"] as IBusinessLogicAccessPoint;
                        }

                if (_businessLogicAccessPoint == null)
                {
                    _businessLogicAccessPoint = GetNewBusinessLogic(DataAccess);

                    SaveToSession(false, true);
                }
                return _businessLogicAccessPoint; 
            }
            set 
            {
                _businessLogicAccessPoint = value;

                SaveToSession(false, true);
            }
        }

        public static IDataAccessAccessPoint GetNewDataLibrary()
        {
            return new EFInstanceProvider();
        }

        public static IBusinessLogicAccessPoint GetNewBusinessLogic(IDataAccessAccessPoint dataAccessProvider)
        {
            return new BusinessLogicAccessPoint(dataAccessProvider);
        }
    }
}
